package com.zhiqianbao.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.KeyEvent;
import android.widget.Toast;

import com.zhiqianbao.entity.UpdateBean;

public class DialogUtils {
    protected static ProgressDialog pd = null;

    /**
     * Toast鎻愮ず
     *
     * @param context
     * @param msg
     */
    public static void showToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    /**
     * Toast鎻愮ず
     *
     * @param context
     * @param msgId
     */
    public static void showToast(Context context, int msgId) {
        Toast.makeText(context, msgId, Toast.LENGTH_LONG).show();
    }


    public static void showDialog(String s, Context c) {
        pd = ProgressDialog.show(c, "", s, true, true, new OnCancelListener() {
            public void onCancel(DialogInterface iDlg) {

            }
        });
    }

    public static Dialog createUpdateDialog(Context context, int iconId, String title, final UpdateBean updateBean,
                                            String positiveBtnName, String negativeBtnName,
                                            DialogInterface.OnClickListener positiveBtnListener,
                                            DialogInterface.OnClickListener negativeBtnListener) {
        Dialog dialog = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(iconId);
        builder.setTitle(title);
        builder.setCancelable(false);
        builder.setMessage(updateBean.getUpdate_content());
        builder.setPositiveButton(positiveBtnName, positiveBtnListener);
        builder.setNegativeButton(negativeBtnName, negativeBtnListener);
        dialog = builder.create();

        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (updateBean.getForce_update().equals("true")) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                }
                return false;
            }
        });
        return dialog;
    }
}
